package com.music.mysongs.db

import android.content.ContentValues
import android.net.Uri
import androidx.core.net.toUri
import com.music.mysongs.bean.SongBean
import com.music.mysongs.utils.MyApplication.Companion.context
import com.music.mysongs.utils.myLog

class MusicDataBaseManager {
    private val mTAG = "MusicDataBaseManager"
    private val dbHelper = MusicDataBase(context, "musicDB.db", 1)
    private var db = dbHelper.writableDatabase

    //最后一条记录的自增键 @MainAct
    fun getDBCount(): Int {
        val cursor = db.rawQuery("select count(id) from musicDB", null)
        if (cursor.moveToNext()) {
            return cursor.getInt(0)
        } else {
            cursor.close()
            return 0
        }
    }

    //创建DB--@MainAct
    fun createDB() {
        dbHelper.writableDatabase
        myLog("$mTAG: createDB success")
    }

    //增DB--@Scan @beanMan
    fun insertDB(bean: SongBean) {
        val insertData = ContentValues().apply {
            put("db_title", bean.title)
            put("db_artist", bean.artist)
            put("db_path", bean.path.toString())
            put("db_duration", bean.duration)
            put("db_webId", bean.webId)
        }
        db.insert("musicDB", null, insertData)
    }

    //删DB--传入的path值 @beanMan
    fun deleteDB(dPath: Uri) {
        val pick = db.delete("musicDB", "db_path=?", arrayOf(dPath.toString()))
        myLog("$mTAG: deleteDB $pick=$dPath")
    }

    //查DB是否存在--传入的path值 @Scan
    fun queryExistDB(dPath: Uri): Boolean {
        val cursor =
            db.query("musicDB", null, "db_path=?", arrayOf(dPath.toString()), null, null, null)
        if (cursor.moveToFirst()) {
            do {
                val getPath = cursor.getString(cursor.getColumnIndex("db_path"))
                //myLog("$mTAG: queryPath:$getPath")
                if (getPath.isNotEmpty()) {
                    //myLog("$mTAG: queryPath exist")
                    return true
                }
            } while (cursor.moveToNext())
        }
        cursor.close()
        return false
    }

    //查DB分类数据--喜欢的歌@beanMan

    fun queryLoveDB(arr: ArrayList<SongBean>) {
        val cursor =
            db.query("musicDB", null, null, null, null, null, null)
        if (cursor.moveToFirst()) {
            do {
                val isLocal = cursor.getInt(cursor.getColumnIndex("db_webId")) == 0
                if (!isLocal) {
                    val bean = SongBean(
                        cursor.getString(cursor.getColumnIndex("db_title")),
                        cursor.getString(cursor.getColumnIndex("db_artist")),
                        cursor.getString(cursor.getColumnIndex("db_path")).toUri(),
                        cursor.getLong(cursor.getColumnIndex("db_duration")),
                        cursor.getInt(cursor.getColumnIndex("db_webId"))
                    )
                    arr.add(bean)
                }
            } while (cursor.moveToNext())
        }
        cursor.close()
    }

    //查DB分类数据--本地的歌@beanMan
    fun queryLocalDB(arr: ArrayList<SongBean>) {
        val cursor =
            db.query("musicDB", null, "db_webId=?", arrayOf("0"), null, null, null)
        if (cursor.moveToFirst()) {
            do {
                val bean = SongBean(
                    cursor.getString(cursor.getColumnIndex("db_title")),
                    cursor.getString(cursor.getColumnIndex("db_artist")),
                    cursor.getString(cursor.getColumnIndex("db_path")).toUri(),
                    cursor.getLong(cursor.getColumnIndex("db_duration")),
                    cursor.getInt(cursor.getColumnIndex("db_webId"))
                )
                arr.add(bean)
            } while (cursor.moveToNext())
        }
        cursor.close()
    }

    /*    //改DB喜欢--传入的path值
    private fun updateLoveDB(dPath: Uri, love: Int) {
        val pick = ContentValues()
        pick.put("db_isWebLove", love)
        db.update("musicDB", pick, "db_path=?", arrayOf(dPath.toString()))
        myLog("$mTAG: updateLoveDB success")
    }*/
    //查DB--传入的path
    /* fun querySongDB(path: Uri): SongBean? {
         val cursor =
             db.query("musicDB", null, "db_path=?", arrayOf(path.toString()), null, null, null)
         var bean: SongBean? = null
         if (cursor.moveToFirst()) {
             //cursor.moveToPosition(index)
             do {
                 bean = SongBean(
                     cursor.getString(cursor.getColumnIndex("db_title")),
                     cursor.getString(cursor.getColumnIndex("db_artist")),
                     cursor.getString(cursor.getColumnIndex("db_path")).toUri(),
                     cursor.getLong(cursor.getColumnIndex("db_duration")),
                     cursor.getInt(cursor.getColumnIndex("db_isWebLove")) == 1
                 )
             } while (cursor.moveToNext())
         }
         cursor.close()
         return bean
     }

     //DB数据是否为空
     private fun isNullDB(): Boolean {
         if (db != null) {
             val cursor = db.query("musicDB", null, null, null, null, null, null)
             if (cursor.moveToFirst()) {
                 return false
             }
             cursor.close()
         }
         return true
     }*/
}